package com.berwin.caper.view.components.filetree;

import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JFileTree extends JTree implements MouseListener {

    private String rootPath = "";
    private JFileTreePopMenu popMenu;

    public JFileTree(String rootPath) {
        super();
        this.rootPath = rootPath;
        this.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        this.addMouseListener(this);
        JFileNode rootNode = new JFileNode(new File(rootPath));
        this.setModel(new DefaultTreeModel(rootNode, false));
        this.handleCurrentDirectory(rootNode);

        //
        this.popMenu = new JFileTreePopMenu(true);


        this.setEditable(true);
        this.addTreeWillExpandListener(new TreeWillExpandListener() {
            @Override
            public void treeWillExpand(TreeExpansionEvent event) {
                TreePath treePath = event.getPath();
                JFileNode rootNode = (JFileNode) treePath.getLastPathComponent();
                JFileTree.this.handleCurrentDirectory(rootNode);
            }

            @Override
            public void treeWillCollapse(TreeExpansionEvent event) {
                TreePath treePath = event.getPath();
                JFileNode rootNode = (JFileNode) treePath.getLastPathComponent();
                rootNode.removeAllChildren();
                rootNode.add(new DefaultMutableTreeNode("加载中..."));
            }
        });
//        // 添加选择监听
//        this.addTreeSelectionListener(new TreeSelectionListener() {
//            @Override
//            public void valueChanged(TreeSelectionEvent e) {
//                System.out.println("当前被选中的节点: " + _treePathsToFilePath(e.getPaths()));
//            }
//        });
    }

    private void handleCurrentDirectory(JFileNode rootNode) {
        rootNode.removeAllChildren();
        List<JFileNode> fileNodes = this.recursiveCurrentFiles(rootNode.getFile());
        for (JFileNode fileNode : fileNodes) {
            rootNode.add(fileNode);
            if (fileNode.isDirectory()) {
                fileNode.add(new DefaultMutableTreeNode("加载中..."));
            }
        }
    }

    /**
     * 递归获取指定文件的路径
     *
     * @param src 指定文件/夹
     */
    private List<JFileNode> recursiveCurrentFiles(File src) {
        List<JFileNode> fileNodes = new ArrayList<>();
        if (src.isDirectory()) {
            File[] files = src.listFiles();
            for (File file : files) {
                fileNodes.add(new JFileNode(file));
            }
        }
        return fileNodes;
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        TreePath path = this.getPathForLocation(e.getX(), e.getY()); // 关键是这个方法的使用
        if (path == null) //JTree上没有任何项被选中
            return;

        this.setSelectionPath(path);

        if (e.getButton() == MouseEvent.BUTTON3) {
            JFileNode rootNode = (JFileNode) path.getLastPathComponent();
            popMenu.show(this, e.getX(), e.getY(), rootNode);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
